<?php

namespace App\Http\Controllers\Dash;

use App\Http\Controllers\Controller;
use App\Model\Blog\Categories;
use App\Model\Blog\CategoriesPost;
use App\Model\Blog\Post;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:download-create', ['only' => ['create', 'store']]);
        $this->middleware('permission:download-delete', ['only' => ['delete']]);
    }

    public function index()
    {
        $data = Post::paginate(5);
        return view('dash.blog.index', compact('data'));
    }

    public function create()
    {
        $cate = Categories::all();
        $categories = [];

        foreach ($cate as $item) {
            $categories = [$item->id, $item->name];
        }

        return view('dash.blog.create', compact('categories'));
    }

    public function store(Request $request)
    {

        if ($request->category == 0) {

            $category = explode(",", $request->category_n);
            $categor = null;
            foreach ($category as $item) {
                $cate = new Categories();
                $cate->name = $item;
                $cate->save();
                $categor[] = $cate->id;
            }
        }

        $data = new Post();
        $data->title = $request->title;
        $data->text = $request->text;
        $data->user_id = auth()->id();
        $data->img = $this->upload($request, 'img');

        $data->save();

        if (is_array($categor)) {
            foreach ($categor as $item) {
                $catx = new CategoriesPost();
                $catx->post_id = $data->id;
                $catx->category_id = $item;
                $catx->save();
            }

        }

        return redirect()->route('dash.blog')->with('success', $data);
    }

    public function edit($id)
    {
        $data = Post::find($id);
        $err = null;
        if ($data->user_id != auth()->id()) {
            $err = 'editable-creator';
        }
        return view('dash.blog.edit', compact('data', 'err', 'id'));
    }

    public function update($id, Request $request)
    {
        $data = Post::find($id);
        if ($data->user_id != auth()->id()) {
            redirect()->route('dash.blog.index');
        }
        $data->title = $request->title;
        $data->text = $request->text;
        $data->img = $request->img;
        $data->save();

        return redirect()->route('dash.blog')->with('success', $data);
    }

    public function delete($id)
    {
        $data = Post::find($id)->delete();
        return redirect()->route('dash.blog')->with('success', $data);
    }

    private function upload($request, $name)
    {
        if ($request->hasFile($name)) {
            //get filename with extension
            $filenamewithextension = $request->file($name)->getClientOriginalName();

            //get filename without extension
            $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

            //get file extension
            $extension = $request->file($name)->getClientOriginalExtension();

            //filename to store
            $filenametostore = $filename . '_' . time() . '.' . $extension;

            //Upload File
            $request->file($name)->storeAs('public/uploads', $filenametostore);

            return asset('storage/uploads/' . $filenametostore);

        }
    }
}
